package com.gy.boot.common.dao;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class IDao<T> extends SqlSessionDaoSupport {

    private static SqlSession sqlSession;

    @Autowired
    public IDao(SqlSessionFactory sqlSessionFactory) {
        System.out.println("===== CommonDao - sqlSession =====");
        super.setSqlSessionFactory(sqlSessionFactory);
        sqlSession = getSqlSession();
    }

/* =============================  静态调用  =============================== */
    public static List<IEntity> listS(String sqlId, Object params) {
        return sqlSession.selectList(sqlId, params);
    }
    public static IEntity getS(String sqlId, Object params) {
        return sqlSession.selectOne(sqlId, params);
    }
    public static int countIS(String sqlId, Object params) {
        return sqlSession.selectOne(sqlId, params);
    }
    public static long countLS(String sqlId, Object params) {
        return sqlSession.selectOne(sqlId, params);
    }
    public static void insertS(String sqlId, Object params) {
        sqlSession.insert(sqlId, params);
    }
    public static void updateS(String sqlId, Object params) {
        sqlSession.update(sqlId, params);
    }
    public static void deleteS(String sqlId, Object params) {
        sqlSession.delete(sqlId, params);
    }

/* =============================  动态调用  =============================== */

    public List<T> list(String sqlId, Object params) {
        return getSqlSession().selectList(sqlId, params);
    }
    public T get(String sqlId, Object params) {
        return getSqlSession().selectOne(sqlId, params);
    }
    public int countI(String sqlId, Object params) {
        return getSqlSession().selectOne(sqlId, params);
    }
    public long countL(String sqlId, Object params) {
        return getSqlSession().selectOne(sqlId, params);
    }
    public void insert(String sqlId, Object params) {
        getSqlSession().insert(sqlId, params);
    }
    public void update(String sqlId, Object params) {
        getSqlSession().update(sqlId, params);
    }
    public void delete(String sqlId, Object params) {
        getSqlSession().delete(sqlId, params);
    }

}
